package com.yiboshi.rehrs.controller;

import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yiboshi.rehrs.domain.entity.ZhongUser;
import com.yiboshi.rehrs.domain.entity.ZhongUserImage;
import com.yiboshi.rehrs.domain.service.IZhongUserImageService;
import com.yiboshi.rehrs.domain.service.IZhongUserService;
import com.yiboshi.rehrs.feign.PosterFeign;
import com.yiboshi.rehrs.feign.PosterFeignInterceptor;
import com.yiboshi.rehrs.helper.ZhongConfigHelper;
import com.yiboshi.rehrs.util.HttpClientUtils;
import com.yiboshi.rehrs.util.JSONBuilder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.http.MediaType;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * @author jack 2025/3/21
 */
@Profile({
    "dev","yibobeta"
})
@RestController
@RequestMapping("/mock/")
@Api(value = "/mock/",tags="A02.AntMockController",description = "A02.业务MOCK接口")
@Slf4j
public class AntMockController {


    @Autowired
    PosterFeign posterFeign;
    @Autowired
    IZhongUserImageService zhongUserImageService;

    @Autowired
    ZhongConfigHelper zhongConfigHelper;

    String account = "215301150016";

    @ApiOperation(value = "1.0 发短信", httpMethod = "POST", notes = "1.0 发短信", consumes = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping("pdalogin/p/common/noTokenAccessSendAuthCodeByUsr")
    public Object sendZhongyou(@RequestBody  JSONObject jsonObject){

        //JSONBuilder person_no = JSONBuilder.start("person_no", account);
        ZhongUser zhongUser =new ZhongUser();
        zhongUser.setAccount(jsonObject.getString("person_no"));
        PosterFeignInterceptor.setHeader(zhongUser);
        JSONObject res = posterFeign.sendauthCodeByUsr(jsonObject);
        log.info("noTokenAccessSendAuthCodeByUsr,res:{}",res);
        return res;
    }

    @ApiOperation(value = "2.0 短信登录", httpMethod = "POST", notes = "2.0 短信登录", consumes = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping("pdalogin/p/common/loginbyusrsms")
    public Object smsLogon(@RequestBody  JSONObject jsonObject){
       /* JSONBuilder person_no = JSONBuilder.start("person_no", account)
                .put("auth_code",authCode)
                .put("telephone","")
                .put("multiple_valued","0,0");*/
        ZhongUser zhongUser =new ZhongUser();
        zhongUser.setAccount(jsonObject.getString("person_no"));
        PosterFeignInterceptor.setHeader(zhongUser);
        JSONObject res = posterFeign.smsLogon(jsonObject);
        log.info("smsLogon,res:{}",res);
        return res;
    }

    @ApiOperation(value = "3.0 人脸登录", httpMethod = "POST", notes = "3.0 人脸登录", consumes = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping("pdalogin/p/common/loginbyusrface")
    public Object faceLogon(@RequestBody  JSONObject jsonObject, HttpServletRequest request){
        String person_no1 = jsonObject.getString("person_no");
        String encode = zhongUserImageService.getOne(person_no1).getImage();
        //byte[] bytes = FileUtil.readBytes("C:\\Users\\472464220\\Desktop\\xiaofeixia\\ym.jpg");
        //String encode = Base64Utils.encodeToString(bytes);
        setMockHeader(request);
        //PosterFeignInterceptor.setHeader(zhongUser);
        //LambdaQueryWrapper<ZhongUserImage> queryWrapper = zhongUserImageService.getQueryWrapper().eq(ZhongUserImage::getAccount, person_no1);
        //zhongUserImageService.
        JSONBuilder person_no = JSONBuilder.start()
                .put("person_no", person_no1)
                .put("multiple_valued","1,1")
                .put("image_face",encode+System.currentTimeMillis())
                ;
        JSONObject res = posterFeign.loginByFace(person_no.end());
        log.info("loginbyusrface,jsonObject:{}",res);
        return res;
    }


    @ApiOperation(value = "4.0 列表查询", httpMethod = "POST", notes = "3.0 列表查询", consumes = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping("delivery-app/p/delivery/pdataskquery/findpdasignwaybillinfonew.do")
    public Object findpdasignwaybillinfonew(@RequestBody  JSONObject jsonObject, HttpServletRequest request){
        setMockHeader(request);
        JSONObject findpdasignwaybillinfonew = posterFeign.findpdasignwaybillinfonew(jsonObject);

        log.info("findpdasignwaybillinfonew,res:{}",findpdasignwaybillinfonew);
        return findpdasignwaybillinfonew;
    }

    @ApiOperation(value = "4.1 修改手机号", httpMethod = "POST", notes = "4.1修改手机号", consumes = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping("delivery-app/p/delivery/cloudcall/updatephone")
    public Object updatephone(@RequestBody  JSONObject jsonObject, HttpServletRequest request){
        setMockHeader(request);
        JSONObject updatephone = posterFeign.updatephone(jsonObject);

        log.info("updatephone,res:{}",updatephone);
        return updatephone;
    }

    @ApiOperation(value = "4.2 查询单", httpMethod = "POST", notes = "4.2 查询单", consumes = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping("delivery-app/p/delivery/cloudcall/querywaybill")
    public Object querywaybill(@RequestBody  JSONObject jsonObject, HttpServletRequest request){
        setMockHeader(request);

        //ZhongUser zhongUser =new ZhongUser();
        //zhongUser.setToken("dab7754bda1e49bc83bc4a30aff089f5");
        //zhongUser.setAccount("2153011500166");
        //zhongUser.setPadId("869296041015581");
        //PosterFeignInterceptor.setHeader(zhongUser);
        JSONObject querywaybill = posterFeign.querywaybill(jsonObject);
        log.info("updatephone,res:{}",querywaybill);
        return querywaybill;
    }

    @ApiOperation(value = "4.3 预约", httpMethod = "POST", notes = "4.3 预约", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @PostMapping("delivery-app/p/delivery/cloudcall/newCallAndFile")
    public Object newCallAndFile(@RequestBody  String jsonObject, HttpServletRequest request) throws Exception {
        setMockHeader(request);
        //JSONObject newCallAndFile = posterFeign.newCallAndFile(jsonObject);
        String queryString = jsonObject;//request.getQueryString();
        Map<String, String> header = PosterFeignInterceptor.getHeader();
        //header.put("user-agent","okhttp/3.10.0");
        //header.put("content-type","application/x-www-form-urlencoded; charset=utf-8");
        log.info("newCallAndFile,header:{}",header);
        //HttpHost proxy = zhongConfigHelper.getProxy();
        HttpResponse httpResponse = HttpClientUtils
                .doPostProxy("http://211.156.197.65/delivery-app/p/delivery/cloudcall/newCallAndFile",header,queryString.getBytes(),null);
               // .doPost("http://211.156.197.65/delivery-app/p/delivery/cloudcall/newCallAndFile", "", header, null, queryString.getBytes());
        JSONObject jsonObject1 = HttpClientUtils.toJson(httpResponse);
        log.info("newCallAndFile,res:{},queryString:{}",jsonObject1,queryString);
        return jsonObject1;
    }

    @ApiOperation(value = "4.4 段信息查询", httpMethod = "POST", notes = "4.4 段信息查询", consumes = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping("delivery-app/p/pdasetroadsegone/queryUnfinishone")
    public Object queryUnfinishone(@RequestBody  String jsonObject, HttpServletRequest request) throws Exception {
        setMockHeader(request);

        String queryString = jsonObject;//request.getQueryString();
        Map<String, String> header = PosterFeignInterceptor.getHeader();
        log.info("queryUnfinishone,header:{}",header);
        HttpResponse httpResponse = HttpClientUtils
                .doPostProxy("http://211.156.197.65/delivery-app/p/pdasetroadsegone/queryUnfinishone",header,queryString.getBytes(),null);
        JSONObject jsonObject1 = HttpClientUtils.toJson(httpResponse);
        log.info("queryUnfinishone,res:{},queryString:{}",jsonObject1,queryString);
        return jsonObject1;
    }

    @ApiOperation(value = "4.5 选择段", httpMethod = "POST", notes = "4.5 选择段", consumes = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping("delivery-app/p/pdasetroadsegone/queryUnfinishtwo")
    public Object queryUnfinishtwo(@RequestBody  String jsonObject, HttpServletRequest request) throws Exception {
        setMockHeader(request);
        String queryString = jsonObject;//request.getQueryString();
        Map<String, String> header = PosterFeignInterceptor.getHeader();
        log.info("queryUnfinishtwo,header:{}",header);
        HttpResponse httpResponse = HttpClientUtils
                .doPostProxy("http://211.156.197.65/delivery-app/p/pdasetroadsegone/queryUnfinishtwo",header,queryString.getBytes(),null);
        JSONObject jsonObject1 = HttpClientUtils.toJson(httpResponse);
        log.info("queryUnfinishtwo,res:{},queryString:{}",jsonObject1,queryString);
        return jsonObject1;
    }

    @ApiOperation(value = "4.6 扫描后根据单号查询/稽核", httpMethod = "POST", notes = "4.6 扫描后根据单号查询/稽核", consumes = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping("pcs-app/p/deliverofficecheck/getCountRecList")
    public Object getCountRecList(@RequestBody  String jsonObject, HttpServletRequest request) throws Exception {
        setMockHeader(request);
        String queryString = jsonObject;//request.getQueryString();
        Map<String, String> header = PosterFeignInterceptor.getHeader();
        log.info("getCountRecList,header:{}",header);
        HttpResponse httpResponse = HttpClientUtils
                .doPostProxy("http://211.156.197.65/pcs-app/p/deliverofficecheck/getCountRecList",header,queryString.getBytes(),null);
        JSONObject jsonObject1 = HttpClientUtils.toJson(httpResponse);
        log.info("getCountRecList,res:{},queryString:{}",jsonObject1,queryString);
        return jsonObject1;
    }

    @ApiOperation(value = "4.7 下段查询", httpMethod = "POST", notes = "4.7 下段查询", consumes = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping("delivery-app/p/pdasetroadsegone/findwaybillnoinfoone")
    public Object findwaybillnoinfoone(@RequestBody  String jsonObject, HttpServletRequest request) throws Exception {
        setMockHeader(request);
        String queryString = jsonObject;//request.getQueryString();
        Map<String, String> header = PosterFeignInterceptor.getHeader();
        log.info("findwaybillnoinfoone,header:{}",header);
        HttpResponse httpResponse = HttpClientUtils
                .doPostProxy("http://211.156.197.65/delivery-app/p/pdasetroadsegone/findwaybillnoinfoone",header,queryString.getBytes(),null);
        JSONObject jsonObject1 = HttpClientUtils.toJson(httpResponse);
        log.info("findwaybillnoinfoone,res:{},queryString:{}",jsonObject1,queryString);
        return jsonObject1;
        /*JSONObject res = new JSONObject();
        res.put("resCode","A00001");
        res.put("obj","[{\"receiptFlag\":\"1\",\"roadSegRes\":\"1\",\"waybillState\":\"10\",\"receiverLinker\":\"史忠杰\",\"receiverAddr\":\"江苏省苏州市昆山市玉山镇新南中路567号2幢103\",\"codFlag\":\"0\",\"codAmount\":0.0,\"oneBillFlag\":\"0\",\"oneBillTotalNumber\":0,\"valuableFlag\":\"0\",\"bizProductName\":\"省内EMS物品次晨达\",\"paymentMode\":\"1\",\"dlvNotes\":\"100\",\"dlvOrgId\":0,\"dlvPersonId\":0,\"resmsg\":\"CCD\",\"bizOccurDateStr\":\"2025-04-14 10:35:47\",\"bizremark\":\"米\",\"alreadySetNum\":0,\"importantFlag\":\"1\",\"importantTips\":\"寄件人要求：<Br>小米项目: 需预约后投递，经客户同意后方可投快递柜或代收点。如客户委托他人代收，务必验证代收人身份，将代收人身份证号后4位记录在签收单上。<Br>\"}]");
        return res;*/
    }

    /*@ApiOperation(value = "4.8 下段回传（下段成功的单）", httpMethod = "POST", notes = "4.8 下段回传（下段成功的单）", consumes = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping("delivery-app/p/pdasetroadsegone/makereceiveone")
    public Object makereceiveone(@RequestBody  String jsonObject, HttpServletRequest request) throws Exception {
        setMockHeader(request);
        String queryString = jsonObject;//request.getQueryString();
        Map<String, String> header = PosterFeignInterceptor.getHeader();
        log.info("makereceiveone,header:{}",header);
       *//* HttpResponse httpResponse = HttpClientUtils
                .doPostProxy("http://211.156.197.65/delivery-app/p/pdasetroadsegone/makereceiveone",header,queryString.getBytes(),null);
        JSONObject jsonObject1 = HttpClientUtils.toJson(httpResponse);
        log.info("makereceiveone,res:{},queryString:{}",jsonObject1,queryString);*//*
        JSONObject res = new JSONObject();
        res.put("resCode","A00001");
        return res;
    }*/

    @ApiOperation(value = "4.8 下段回传（下段成功的单）", httpMethod = "POST", notes = "4.8 下段回传（下段成功的单）", consumes = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping("delivery-app/p/pdasetroadsegone/makereceiveonemixed")
    public Object makereceiveonemixed(@RequestBody  String jsonObject, HttpServletRequest request) throws Exception {
        setMockHeader(request);
        String queryString = jsonObject;//request.getQueryString();
        Map<String, String> header = PosterFeignInterceptor.getHeader();
        log.info("makereceiveonemixed,header:{}",header);
        HttpResponse httpResponse = HttpClientUtils
                .doPostProxy("http://211.156.197.65/delivery-app/p/pdasetroadsegone/makereceiveonemixed",header,queryString.getBytes(),null);
        JSONObject jsonObject1 = HttpClientUtils.toJson(httpResponse);
        log.info("makereceiveonemixed,res:{},queryString:{}",jsonObject1,queryString);
        return jsonObject1;
       /* JSONObject res = new JSONObject();
        res.put("resCode","A00001");
        return res;*/
    }



    @ApiOperation(value = "4.9 妥投", httpMethod = "POST", notes = "4.9 妥投", consumes = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping("delivery-app/p/pdasignwaybillFeedback/batchallDeliver")
    public Object batchallDeliver(@RequestBody  String jsonObject, HttpServletRequest request) throws Exception {
        setMockHeader(request);
        String queryString = jsonObject;//request.getQueryString();
        Map<String, String> header = PosterFeignInterceptor.getHeader();
        log.info("batchallDeliver,header:{}",header);
        HttpResponse httpResponse = HttpClientUtils
                .doPostProxy("http://211.156.197.65/delivery-app/p/pdasignwaybillFeedback/batchallDeliver",header,queryString.getBytes(),null);
        JSONObject jsonObject1 = HttpClientUtils.toJson(httpResponse);
        log.info("batchallDeliver,res:{},queryString:{}",jsonObject1,queryString);
        return jsonObject1;
    }

    @ApiOperation(value = "5.0 退回寄件人", httpMethod = "POST", notes = "5.0 退回寄件人", consumes = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping("delivery-app/p/delivery/pdasignwaybillreturned/completeConfirmBatch")
    public Object completeConfirmBatch(@RequestBody  String jsonObject, HttpServletRequest request) throws Exception {
        setMockHeader(request);
        String queryString = jsonObject;//request.getQueryString();
        Map<String, String> header = PosterFeignInterceptor.getHeader();
        log.info("completeConfirmBatch,header:{}",header);
        HttpResponse httpResponse = HttpClientUtils
                .doPostProxy("http://211.156.197.65/delivery-app/p/delivery/pdasignwaybillreturned/completeConfirmBatch",header,queryString.getBytes(),null);
        JSONObject jsonObject1 = HttpClientUtils.toJson(httpResponse);
        log.info("completeConfirmBatch,res:{},queryString:{}",jsonObject1,queryString);
        return jsonObject1;
    }

    //mock
    @ApiOperation(value = "6.0 转次日投", httpMethod = "POST", notes = "6.0 转次日投", consumes = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping("delivery-app/p/delivery/nondeliveryrec/querynondeliveryrec")
    public Object querynondeliveryrec(@RequestBody  String jsonObject, HttpServletRequest request) throws Exception {
        setMockHeader(request);
        String queryString = jsonObject;//request.getQueryString();
        Map<String, String> header = PosterFeignInterceptor.getHeader();
        log.info("querynondeliveryrec,header:{}",header);
        HttpResponse httpResponse = HttpClientUtils
                .doPostProxy("http://211.156.197.65/delivery-app/p/delivery/nondeliveryrec/querynondeliveryrec",header,queryString.getBytes(),null);
        JSONObject jsonObject1 = HttpClientUtils.toJson(httpResponse);
        log.info("querynondeliveryrec,res:{},queryString:{}",jsonObject1,queryString);
        return jsonObject1;
    }

    @ApiOperation(value = "7.0 更换替班", httpMethod = "POST", notes = "7.0 更换替班", consumes = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping("pdalogin/p/common/changesubstitutionorg")
    public Object changesubstitutionorg(@RequestBody  String jsonObject, HttpServletRequest request) throws Exception {
        setMockHeader(request);
        String queryString = jsonObject;//request.getQueryString();
        Map<String, String> header = PosterFeignInterceptor.getHeader();
        log.info("changesubstitutionorg,header:{}",header);
        HttpResponse httpResponse = HttpClientUtils
                .doPostProxy("http://211.156.197.65/pdalogin/p/common/changesubstitutionorg",header,queryString.getBytes(),null);
        JSONObject jsonObject1 = HttpClientUtils.toJson(httpResponse);
        log.info("changesubstitutionorg,res:{},queryString:{}",jsonObject1,queryString);
        return jsonObject1;
    }



    @ApiOperation(value = "7.1 合并打卡", httpMethod = "POST", notes = "7.1 合并打卡", consumes = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping("/adt-app/p/clockInForFieldNew/findByKeyPointQuery")
    public Object findByKeyPointQuery(@RequestBody  String jsonObject, HttpServletRequest request) throws Exception {
        setMockHeader(request);
        String queryString = jsonObject;//request.getQueryString();
        Map<String, String> header = PosterFeignInterceptor.getHeader();
        log.info("findByKeyPointQuery,header:{}",header);
        HttpResponse httpResponse = HttpClientUtils
                .doPostProxy("http://211.156.197.65/adt-app/p/clockInForFieldNew/findByKeyPointQuery",header,queryString.getBytes(),null);
        JSONObject jsonObject1 = HttpClientUtils.toJson(httpResponse);
        log.info("findByKeyPointQuery,res:{},queryString:{}",jsonObject1,queryString);
        return jsonObject1;
    }





    private void setMockHeader(HttpServletRequest request) {
        Enumeration<String> headerNames = request.getHeaderNames();
        Map<String,String> map = new HashMap<>();
        while(headerNames.hasMoreElements()){
            String headerName = headerNames.nextElement();
            Enumeration<String> headers = request.getHeaders(headerName);
            if(headers.hasMoreElements()){
                String value = headers.nextElement();
                map.put(headerName,value);
                //log.info("{},{}",headerName,value);
            }
        }
        map.put(	"host", "211.156.197.65");
        map.remove("referer");
        map.remove("origin");
        map.remove("host");
        map.remove("content-length");
        map.remove("");
        map.remove("accept-encoding");
        map.put("user-agent","Dalvik/2.1.0 (Linux; U; Android 9;NLS-MT90 Build/PQ3A.190801.002)");
        //log.info("header map,{}",map);
        PosterFeignInterceptor.setHeader(map);
    }


    public static void main(String[] args) {
        Map<String,String> map = new HashMap<>();
        map.put("a-cd","5");
        String s = map.get("A-cd");
        System.out.println(s);
    }


}
